package com.hspedu.method;

/**
 * @author LiuJinTao
 * @className ThreadMethod03
 * @date 2024/06/23 17:15
 * @description TODO
 */
public class ThreadMethod03 {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new MyDaemonThread());
        // 如果我们希望当 main 线程结束后 子线程自动结束
        // 只需将子线程设为守护线程
        thread.setDaemon(true);
        thread.start();

        for (int i = 1; i <= 10; i++) {
            System.out.println("main main");
            Thread.sleep(1000);
        }
    }
}

// Daemon 守护线程，守护进程，守护程序
class MyDaemonThread implements Runnable {
    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("hhhhhhhhhhh");
        }
    }
}